var a, t, i, e, n, s, d, o, r, p, c, h,
    u = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (a) {
        return typeof a;
    } : function (a) {
        return a && "function" == typeof Symbol && a.constructor === Symbol && a !== Symbol.prototype ? "symbol" : typeof a;
    }, _ = (require("../../wxParse/wxParse.js"), getApp()), l = {
        consignee: 1,
        shipping_id: [],
        msg: ""
    }, f = new Array();

Page({
    data: {
        index: 0,
        addresss_link: "../address/index?from=flow",
        checkList: [],
        shipping_id: 0,
        shopLists: "",
        resShipingId: [],
        payfee: [],
        addresss: "",
        maskVisual: "hidden",
        current: 1,
        hiddenOrder: !0,
        hiddenAddress: !0,
        hiddenUser: !0,
        hiddenUnit: !0,
        is_first_action: !0,
        temp: [],
        isShow_01: false,
        listData_01: [[
            '次日 7:00 - 7:30',
            '次日 7:30 - 8:00',
            '次日 8:00 - 8:30',
            '次日 8:30 - 9:00',
            '次日 9:00 - 9:30',
            '次日 9:30 - 10:00',
            '次日 10:00 - 10:30',
            '次日 10:30 - 11:00',
            '次日 11:00 - 11:30',
            '次日 11:30 - 12:00',
            '次日 12:00 - 12:30',
            '次日 12:30 - 13:00',
            '次日 13:00 - 13:30',
            '次日 13:30 - 14:00',
            '次日 14:00 - 14:30',
            '次日 14:30 - 15:00',
            '次日 15:00 - 15:30',
            '次日 15:30 - 16:00',
            '次日 16:00 - 16:30',
            '次日 16:30 - 17:00',
            '次日 17:00 - 17:30',
            '次日 17:30 - 18:00',
            '次日 18:00 - 18:30',
            '次日 18:30 - 19:00',
            '次日 19:00 - 19:30',
            '次日 19:30 - 20:00',
            '次日 20:00 - 20:30',
            '次日 20:30 - 21:00',
            '次日 21:00 - 21:30',
            '次日 21:30 - 22:00'
        ]],
        picker_01_data: '',
        distanceIn:false,
        cou_id:0,
        checkedCou:'使用优惠券'
    },
    onLoad: function (a) {
        console.log(_.globalData);
        r = a.objectId, p = a.flow_type, c = a.team_id, h = a.t_id;
    },
    onShow: function (i) {
        var e = this;var that = this;
        a = wx.getStorageSync("token"), e.setData({
            is_first_action: !0,
            flow_type: p || 0,
            cou_id:0,
            checkedCou:'使用优惠券'
        }), wx.request({
            url: _.apiUrl("flow"),
            method: "post",
            data: {
                flow_type: p || 0,
                bs_id: r || 0,
                team_id: c || 0,
                t_id: h || "0"
            },
            header: {
                "Content-Type": "application/json",
                "X-ECTouch-Authorization": a
            },
            success: function (a) {
                //console.log(a);return ;
                setTimeout(function () {
        
                }, 1e3), l.consignee = a.data.data.default_address.address_id, t = a.data.data.cart_goods_list.list;
                if(t.length == undefined || !t.length){
                    wx.switchTab({
                        url: "../flow/index",
                        success: (result) => {
                            
                        },
                        fail: () => {},
                        complete: () => {}
                    });
                    return ;
                }
                for (var i in t) a.data.data.cart_goods_list[i];
                var n, s = "";
                for (var i in a.data.cart_goods_list) {
                    n = a.data.cart_goods_list.list[0][i].goods_attr.split("\n");
                    for (var d in n) "" != n[d] && (s += n[d] + ",");
                    a.data.cart_goods_list.list[0][i].goods_attr = s.substring(0, s.length - 1);
                }
                console.log(a)
                e.setData({
                    addresss: a.data.data.default_address,
                    shopLists: a.data.data.cart_goods_list,
                    coupons_list: a.data.data.coupons_list,
                    cont_data: a.data.data,
                    shippingFee: a.data.data.cart_goods_list.shippingFee,
                    total: a.data.data.cart_goods_list.order_total/*  + a.data.data.cart_goods_list.shippingFee */
                }), e.getShopName();
                if(a.data.data.cart_goods_list.coupon_checked != ''){
                   e.setData({
                        checkedCou:a.data.data.cart_goods_list.coupon_checked,
                        cou_id: a.data.data.cart_goods_list.coupon_id
                   })
                }
                if(-1 == a.data.data.code){
                    wx.showModal({
                        title: a.data.data.msg,
                        content: '',
                        showCancel: false,
                        cancelText: '取消',
                        cancelColor: '#000000',
                        confirmText: '确定',
                        confirmColor: '#3CC51F',
                        success: (result) => {
                            if (result.confirm) {
                                wx.navigateTo({
                                    url: "../address/index"
                                });
                            }
                        },
                        fail: () => {},
                        complete: () => {}
                    });
                      
                     return;
                 }
                wx.request({
                    url: getApp().apiUrl("user/address/checkDeliverDistance"),
                    method: "post",
                    header: {
                        "X-ECTouch-Authorization": wx.getStorageSync("token")
                    },
                    data: {
                      latitude: e.data.addresss.latitude,
                      longitude: e.data.addresss.longitude
                    },
                    success: function(a) {
                      console.log(a);
                        if(a.data.data == 0){
                            wx.showModal({
                                title: '提示',
                                content: '很抱歉，当前收货地址暂不支持配送！',
                                confirmText:'修改地址',
                                cancelText:'取消',
                                success (res) {
                                    if (res.confirm) {
                                        wx.navigateTo({
                                            url:that.data.addresss_link
                                        });
                                    } else if (res.cancel) {
                                        return;
                                    }
                                }
                            })
                        }else{
                          that.setData({
                            distanceIn:true
                         })
                        }
                    }
                });
                //for (var i in e.data.shippingName) e.shippingChange(new Array(i, e.data.shippingName[i].id));
            }
        }), this.loadingChange();
    },
    onReady: function () {
        var that = this;
        //获得dialog组件
        that.noStockdialog = that.selectComponent("#noStockdialog");
        //         console.log(that.dialog);
    },
    showPicker_01: function () {
        this.setData({
            isShow_01: true
        })
    },
    sureCallBack_01(e) {
        console.log(e)
        this.setData({
            isShow_01: false,
            picker_01_data: e.detail.choosedData[0],
            picker_01_index: JSON.stringify(e.detail.choosedIndexArr)
        })
        /*wx.pageScrollTo({scrollTop:1500,duration:300});*/
    },
    cancleCallBack_01() {
        this.setData({
            isShow_01: false,
        })
    },
    loadingChange: function () {
        var a = this;
        setTimeout(function () {
            a.setData({
                hidden: !0
            });
        }, 2e3);
    },
    getShopName: function (a) {
        var t = this, i = [], e = [];
        for (var n in this.data.shopLists.list) !function (a) {
            var n = [], s = [], d = {
                option: [],
                id: 0
            }, o = t.data.shopLists.list[a];
            "" != o.shop_info && (o.shop_info.forEach(function (a) {
                n.push(a.shipping_name), s.push(a.shipping_id);
            }), d.option = n, i.push(d), e.push(s), l.shipping_id = s[0], t.data.resShipingId.push({
                ru_id: o.shop_info[0].ru_id,
                shipping_id: o.shop_info[0].shipping_id
            }));
        }(n);
        this.setData({
            shippingName: i,
            shpppingId: e
        });
    },
    onChangeShowCoupons: function (a) {
        var t = this;
        t.setData({
            showViewCoupons: !t.data.showViewCoupons
        });
    },
    radioChangeCoupons: function (a) {
        console.log(a);
        var t = this;
        d = a.detail.value;
        var i = wx.getStorageSync("token");
        wx.request({
            url: _.apiUrl("flow/changecou"),
            data: {
                uc_id: a.detail.value,
                flow_type: t.data.cont_data.flow_type
            },
            method: "POST",
            header: {
                "Content-Type": "application/json",
                "X-ECTouch-Authorization": i
            },
            success: function (a) {
                t.setData({
                    cou_money: a.data.data.cou_money,
                    cou_id: a.data.data.cou_id,
                    total: a.data.data.order_total //+ t.data.shippingFee
                });
            }
        });
    },
    radioCheck:function(e){
        console.log(e);
        var that = this;
        var index = e.currentTarget.dataset.index;
        var cou = e.currentTarget.dataset.value;
        var coups = this.data.coupons_list;
        var checked = !coups[index].checked;
        for(var c in coups){
            coups[c].checked = false;
        }
        this.setData({
            coupons_list:coups
        });
        var ss = "coupons_list["+index+"].checked";
        this.setData({
            [ss]:checked
        });
        if(checked == false){
            cou = 0;
            wx.showToast({
                title: '已取消优惠券',
                icon: 'none',
                image: '',
                duration: 1500,
                mask: false,
                success: (result) => {
                    
                },
                fail: () => {},
                complete: () => {}
            });
            that.setData({
                checkedCou:'使用优惠券'
           })    
        }else{
            that.setData({
                checkedCou:that.data.coupons_list[index].cou_title
            })      
        }
        var i = wx.getStorageSync("token");
        wx.request({
            url: _.apiUrl("flow/changecou"),
            data: {
                uc_id: cou,
                flow_type: that.data.cont_data.flow_type
            },
            method: "POST",
            header: {
                "Content-Type": "application/json",
                "X-ECTouch-Authorization": i
            },
            success: function (a) {
                that.setData({
                    cou_money: a.data.data.cou_money,
                    cou_id: cou,
                    total: a.data.data.order_total, //+ t.data.shippingFee,
                    "shopLists.order_total_formated":a.data.data.order_total_formated
                });
            }
        });
    },
    shippingChange: function (t) {
        var i = this;
        if ("object" == (void 0 === t ? "undefined" : u(t)) && 2 == t.length) var e = t[0],
            n = t[1]; else var e = t.currentTarget.dataset.index, n = t.detail.value;
        i.data.shippingName[e].id = n, l.shipping_id = this.data.shpppingId[e][n];
        this.data.shopLists.list[e].shop_list[0].ru_id;
        var s = 0;
        this.data.resShipingId[e].shipping_id = this.data.shopLists.list[e].shop_info[n].shipping_id,
            this.setData({
                shippingName: this.data.shippingName,
                resShipingId: this.data.resShipingId
            }), wx.request({
                url: _.apiUrl("flow/shipping"),
                data: {
                    address: l.consignee,
                    id: this.data.resShipingId[e].shipping_id,
                    ru_id: this.data.resShipingId[e].ru_id,
                    flow_type: p || "0",
                    uc_id: i.data.cou_id ? i.data.cou_id : 0
                },
                method: "POST",
                header: {
                    "Content-Type": "application/json",
                    "X-ECTouch-Authorization": a
                },
                success: function (a) {
                    f[i.data.resShipingId[e].ru_id] = {
                        shipping_id: i.data.resShipingId[e].shipping_id,
                        shipping_fee: parseFloat(a.data.data.fee)
                    }, i.data.payfee.hasOwnProperty(e), a.data.data.fee ? i.data.payfee.splice(e, 1, a.data.data.fee) : i.data.payfee.splice(e, 1, a.data.data.massage),
                        1 == a.data.data.error && wx.showToast({
                            title: a.data.data.massage,
                            image: "../../images/failure.png",
                            duration: 2e3
                        }), i.data.payfee.forEach(function (a) {
                            isNaN(a) || (s += parseFloat(a));
                        }), i.setData({
                            payfee: i.data.payfee,
                            payfee_total: s,
                            total: parseFloat(i.data.shopLists.order_total) + s - parseFloat(i.data.cou_money ? i.data.cou_money : 0),
                            payfee_error: a.data.data.error
                        });
                }
            });
    },
    submitOrder: function (event) {
        console.log(event);
        var that = this;
        if(that.data.distanceIn === false){
            wx.showModal({
                title: '提示',
                content: '很抱歉，当前收货地址暂不支持配送！',
                confirmText:'修改地址',
                cancelText:'取消',
                success (res) {
                    if (res.confirm) {
                        wx.navigateTo({
                            url:that.data.addresss_link
                        });
                    } else if (res.cancel) {
                        return;
                    }
                }
            })
           return; 
        }
        if (that.data.inNight == 1 && that.data.picker_01_data != '') {
            that.makeOrder(event);
            return true;
        }
        wx.request({
            url: _.apiUrl("index/inNight"),
            method: "post",
            data: {},
            header: {
                "Content-Type": "application/json"
            },
            success: function (res) {
                if (res.data.code == 0) {
                    that.setData({
                        inNight: 1,
                        listData_01: res.data.data
                    });
                    that.showPicker_01();
                } else {
                    that.setData({
                        inNight: 0,
                        picker_01_data: ''
                    });
                    that.makeOrder(event);
                }
            }
        });
    },

    showNoStockDialog() {
        this.noStockdialog.showDialog();
    },

    //取消事件
    _cancelNoStockEvent() {
        this.noStockdialog.hideDialog();
    },
    //确认事件
    _confirmNoStockEvent() {
        console.log(this.data.noStockRec);
        var that = this;
        var token = wx.getStorageSync("token");
        wx.request({
            url: getApp().apiUrl("cart/delete"),
            data: {
                rec_id: that.data.noStockRec,
                type:'multi'
            },
            header: {
                "Content-Type": "application/json",
                "X-ECTouch-Authorization": token
            },
            method: "POST",
            success: function (res) {
                that.noStockdialog.hideDialog();
                that.onShow();
            }
        });
       
    },
    makeOrder: function (event) {
        var that = this;
        var token = wx.getStorageSync("token");

         console.log(d);
        var t = that;
        // if (1 == t.data.is_first_action) {
        if (true) {
            if (t.setData({
                is_first_action: !1
            }), 1 == t.data.payfee_error) return void wx.showToast({
                title: "地区不支持配送，无法提交",
                image: "../../images/failure.png",
                duration: 2e3
            });
            if ("" == l.consignee || void 0 == l.consignee) return void _.shwomessage("没有收货地址");
            r = {
                inv_payee: "",
                inv_content: ""
            };
            if (0 == i) {
                if ("单位" == e) r = {
                    invoice_type: i,
                    inv_payee: o,
                    tax_id: n,
                    inv_content: s
                };
                if ("个人" == e) r = {
                    invoice_type: i,
                    inv_payee: e,
                    inv_content: s
                };
            }
            if (1 == i) var r = {
                invoice_type: i,
                vat_id: t.data.invoice_id,
                inv_payee: "",
                inv_content: "",
                tax_id: ""
            };

            wx.request({
                url: _.apiUrl("flow/down"),
                method: "post",
                data: {
                    consignee: l.consignee,
                    shipping: that.data.resShipingId,
                    postdata: r,
                    uc_id: that.data.cou_id || "0",
                    bs_id: t.data.cont_data.bs_id,
                    flow_type: t.data.cont_data.flow_type,
                    team_id: t.data.cont_data.team_id,
                    t_id: t.data.cont_data.t_id,
                    pay_note: that.data.picker_01_data,
                    order_remarks: event.detail.value.order_remarks
                },
                header: {
                    "Content-Type": "application/json",
                    "X-ECTouch-Authorization": a
                },
                success: function (a) {
                    var t = a.data.data;
                        
                    if (a.data.code > 0) {
                        if (a.data.code == 2) {
                            wx.showModal({
                                title: '提示',
                                content: a.data.data,
                                showCancel: false,
                                success(res) {
                                    if (res.confirm) {
                                        wx.redirectTo({
                                            url: '/pages/order/index?id=1'
                                        })
                                    } else if (res.cancel) {
                                        console.log('用户点击取消')
                                    }
                                }
                            })

                        }else if (a.data.code == 3) {
                            var dialogContent = []
                            var noStockRec = [];
                            for (var gg in a.data.data) {
                                dialogContent[gg] = a.data.data[gg].goods_name + '\n';
                                noStockRec[gg] = a.data.data[gg].rec_id;
                            }
                            that.setData({
                                dialogContent: dialogContent,
                                dialogTitle: '以下商品库存不足',
                                noStockRec:noStockRec
                            });
                            that.showNoStockDialog();

                        }else if (a.data.code == 4) {
                            wx.redirectTo({
                                url: "../flow/index"
                            });
                        } else {
                            wx.showToast({
                                title: a.data.data,
                                icon: 'none',
                                image: '',
                                duration: 1500,
                                mask: false,
                                success: (result) => {
                                    
                                },
                                fail: () => {},
                                complete: () => {}
                            });
                        }
                    } else {
                        wx.redirectTo({
                            url: "../flow/done?id=" + t
                        });
                    }

                }
            });
        }
        return true;
    },
    cascadePopup: function () {
        var a = wx.createAnimation({
            duration: 100,
            timingFunction: "ease-in-out"
        });
        this.animation = a, a.translateX(-1e3).step(), this.setData({
            animationData: this.animation.export(),
            maskVisual: "show"
        });
    },
    cascadeDismiss: function () {
        this.animation.translateX(1e3).step(), this.setData({
            animationData: this.animation.export(),
            maskVisual: "hidden"
        });
    },
    bindHeaderTap: function (a) {
        this.setData({
            current: a.target.dataset.index
        }), page = 1, sort_key = a.target.dataset.index;
    },
    toOrder: function (a) {
        i = a.currentTarget.id, this.setData({
            hiddenOrder: !1,
            hiddenAddress: !0,
            invoiceType: a.currentTarget.id
        });
    },
    toAddress: function (a) {
        i = a.currentTarget.id, this.setData({
            hiddenOrder: !0,
            hiddenAddress: !1,
            invoiceType: a.currentTarget.id
        });
    },
    userList: function (a) {
        e = a.currentTarget.id, this.setData({
            hiddenUser: !1,
            hiddenUnit: !0,
            inv_payee: a.currentTarget.id
        });
    },
    unitList: function (a) {
        e = a.currentTarget.id, this.setData({
            hiddenUser: !0,
            hiddenUnit: !1,
            inv_payee: a.currentTarget.id
        });
    },
    unitNameInput: function (a) {
        o = a.detail.value, this.setData({
            unitName: a.detail.value
        });
    },
    unitNumInput: function (a) {
        n = a.detail.value, this.setData({
            headingCode: a.detail.value
        });
    },
    radioChange: function (a) {
        console.log(a);
        s = a.detail.value;
    },
    toAddressTs: function (a) {
        wx.showModal({
            title: "提示",
            content: "你还没有增值发票，去添加吗？",
            success: function (a) {
                a.confirm ? wx.navigateTo({
                    url: "../invoice/create"
                }) : a.cancel;
            }
        });
    },
    commonNav: function () {
        var a = this;
        a.setData({
            nav_select: !a.data.nav_select
        });
    },
    nav: function (a) {
        var t = a.currentTarget.dataset.index;
        "home" == t ? wx.switchTab({
            url: "../index/index"
        }) : "fenlei" == t ? wx.switchTab({
            url: "../category/index"
        }) : "cart" == t ? wx.switchTab({
            url: "../flow/index"
        }) : "profile" == t && wx.switchTab({
            url: "../user/index"
        });
    },
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    }
});